package view.history 
{
	import controller.Const;
	import controller.RequestController;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import model.GameData;
	import model.MyData;
	import view.Base;
	
	/**
	 * ...
	 * @author @Boy_Kute
	 */
	public class HistoryBet extends Base 
	{
		private var _popUpHistory:MovieClip;
		private var _historyDesBetting:MovieClip;
		private var _historyDesCL:MovieClip;
		
		private var _btnLichSuBat:MovieClip;
		private var _btnChanLe:MovieClip;
		private var _btnNext:MovieClip;
		private var _btnPre:MovieClip;
		private var _btnClose:SimpleButton;
		
		private var _arrText:Array = [];
		private var _arrInfoTextResult:Array = [];
		private var _arrHistoryBet:Array = [];
		private var _arrHistoryCL:Array = [];
		private var _checkDefault:Boolean = true;
		
		private var _sumResultAnimal:int = 0;
		private var _sumResultCL:int = 0;
		private var _arrCheckResult:Array = [];
		private var _arrValueCheckResult:Array = [];
		public function HistoryBet() 
		{
			_shadow = new Sprite();
			_shadow.graphics.beginFill(0x000000, .3);
			_shadow.graphics.drawRect(0, 0, 1499, 900);
			_shadow.graphics.endFill();
			addChild(_shadow);
			
			_popUpHistory = getImage("Popup_LichSuBat", "playGame");
			addChild(_popUpHistory)
			_popUpHistory.x = (1400 - _popUpHistory.width) / 2;
			_popUpHistory.y = (900 - _popUpHistory.height) / 2;
			
			_btnLichSuBat = _popUpHistory.btnLichSuBat;
			_btnChanLe = _popUpHistory.btnChanLe;
			_btnNext = _popUpHistory.btnNext;
			_btnPre = _popUpHistory.btnBack;
			_btnClose = _popUpHistory.btnDong;
			
			_btnNext.gotoAndStop(1);
			_btnPre.gotoAndStop(1);
			
			_btnLichSuBat.addEventListener(MouseEvent.CLICK, clickLickSuBat);
			_btnChanLe.addEventListener(MouseEvent.CLICK, clickLickCL);
			_btnNext.addEventListener(MouseEvent.CLICK, clickNext);
			_btnPre.addEventListener(MouseEvent.CLICK, clickPre);
			_btnClose.addEventListener(MouseEvent.CLICK, clickClose);
			
		}
		
		private function clickClose(e:MouseEvent):void 
		{
			dispatchEvent(new Event("closeHistory"));
		}
		
		private function clickPre(e:MouseEvent):void 
		{
			if (_checkDefault)
			{
				removeAllChildBetting();
			}
			else
			{
				removeAllChildCL();
			}
			if (MyData.getInstance().page >= 0)
			{
				var obj:Object = new Object();
				obj[Const.PLAYER_NAME] = MyData.getInstance().myName;
				obj[Const.SEND_GAME_ID] = GameData.getInstance().idGame;
				MyData.getInstance().page -= 1;
				RequestController.getInstance().sendDataHistory(obj);
			}
			
		}
		
		private function clickNext(e:MouseEvent):void 
		{
			if (_checkDefault)
			{
				removeAllChildBetting();
			}
			else
			{
				removeAllChildCL();
			}
			
			var obj:Object = new Object();
			obj[Const.PLAYER_NAME] = MyData.getInstance().myName;
			obj[Const.SEND_GAME_ID] = GameData.getInstance().idGame;
			MyData.getInstance().page += 1;
			RequestController.getInstance().sendDataHistory(obj);
		}
		
		private function clickLickCL(e:MouseEvent):void 
		{
			removeAllChildCL();
			removeAllChildBetting();
			_checkDefault = false;
			var obj:Object = new Object();
			obj[Const.PLAYER_NAME] = MyData.getInstance().myName;
			obj[Const.SEND_GAME_ID] = GameData.getInstance().idGame;
			MyData.getInstance().page = 0;
			RequestController.getInstance().sendDataHistory(obj);
		}
		
		private function clickLickSuBat(e:MouseEvent):void 
		{
			removeAllChildCL();
			removeAllChildBetting();
			_checkDefault = true;
			var obj:Object = new Object();
			obj[Const.PLAYER_NAME] = MyData.getInstance().myName;
			obj[Const.SEND_GAME_ID] = GameData.getInstance().idGame;
			MyData.getInstance().page = 0;
			RequestController.getInstance().sendDataHistory(obj);
		}
		
		public function showInfo(arrInfo:Array):void
		{
			var i:int;
			var j:int;
			_sumResultAnimal = 0;
			_sumResultCL = 0;
			if (MyData.getInstance().page >= 0)
			{
				if (_checkDefault)
				{
					_popUpHistory.gotoAndStop(1);
					_arrText = [];
					_arrInfoTextResult = [];
					_arrHistoryBet = [];
					
					if (arrInfo && arrInfo.length > 0)
					{
						for (i = 0; i < arrInfo.length; i++) 
						{
							_sumResultAnimal = 0;
							_historyDesBetting = getImage("HistoryDesBetting", "playGame");
							addChild(_historyDesBetting);
							
							_arrInfoTextResult = String(arrInfo[i]["bet"]).split(",");
							_arrValueCheckResult = String(arrInfo[i]["game-res"]).split(",");
							_arrText = [_historyDesBetting.txtL1, _historyDesBetting.txtL2, _historyDesBetting.txtL3, _historyDesBetting.txtL4,
										_historyDesBetting.txtL5, _historyDesBetting.txtL6, _historyDesBetting.txtL7, _historyDesBetting.txtL8, 
										_historyDesBetting.txtL9, _historyDesBetting.txtL10];
							
							_arrCheckResult = [	_historyDesBetting.t1, _historyDesBetting.t2, _historyDesBetting.t3, _historyDesBetting.t4,
												_historyDesBetting.t5, _historyDesBetting.t6, _historyDesBetting.t7, _historyDesBetting.t8,
												_historyDesBetting.t9, _historyDesBetting.t10];
							for (j = 0; j < _arrInfoTextResult.length - 2; j++) 
							{
								_sumResultAnimal = _sumResultAnimal + int(_arrInfoTextResult[j]);
								_historyDesBetting.txtTotal.text = String(_sumResultAnimal);
								
								_arrText[j].text = String(_arrInfoTextResult[j]);
								_arrCheckResult[j].gotoAndStop(1);
								_arrCheckResult[j].visible = false;
								
								_arrText[j].selectable = false;
								_historyDesBetting.txtTotal.selectable = false;
								
								if (_arrValueCheckResult[j] > 0 && _arrValueCheckResult[j] <=1)
								{
									trace("= 1", j)
									_arrCheckResult[j].visible = true;
									_arrCheckResult[j].gotoAndStop(1);
								}
								
								if (_arrValueCheckResult[j] > 1 && _arrValueCheckResult[j] <=2)
								{
									_arrCheckResult[j].visible = true;
									_arrCheckResult[j].gotoAndStop(2);
								}
								
								if (_arrValueCheckResult[j] > 2 && _arrValueCheckResult[j] <=3)
								{
									_arrCheckResult[j].visible = true;
									_arrCheckResult[j].gotoAndStop(3);
								}
							}
							
							//_historyDesBetting.txtP.text = (i + 1) + 5 * (MyData.getInstance().page - 1);
							_historyDesBetting.txtP.text = (i + 1) + 5 * (MyData.getInstance().page);
							_historyDesBetting.txtT.text = String(new Date(Number(arrInfo[i]["bet-time"]) * 1000));
							trace("new Date", new Date(Number(arrInfo[i]["bet-time"])))
							_arrHistoryBet.push(_historyDesBetting);
							_historyDesBetting.x = (1400 - _historyDesBetting.width) / 2 - 2;
							_historyDesBetting.y = 370 + (i * (_historyDesBetting.height + 10));
						}
					}
				}
				else
				{
					_popUpHistory.gotoAndStop(2);
					
					_arrText = [];
					_arrInfoTextResult = [];
					_arrHistoryCL = [];
					if (arrInfo && arrInfo.length > 0)
					{
						for (i = 0; i < arrInfo.length; i++) 
						{
							_sumResultCL = 0;
							_historyDesCL = getImage("HistoryDesCL", "playGame");
							addChild(_historyDesCL);
							
							_arrInfoTextResult = String(arrInfo[i]["bet"]).split(",");
							_arrText = [_historyDesCL.txtChan, _historyDesCL.txtLe];
							
							if (_arrText && _arrText.length <= 2)
							{
								for (j = 0; j < _arrText.length; j++) 
								{
									_sumResultCL = _sumResultCL + int(_arrInfoTextResult[j + 10]);
									_arrText[j].text = String(_arrInfoTextResult[j + 10]);
									_arrText[j]..selectable = false;
									
								}
							}
							_historyDesCL.txtTotal.text = String(_sumResultCL);
							_historyDesCL.txtP.text = (i + 1) + 5 * (MyData.getInstance().page);
							_historyDesCL.txtT.text = String(new Date(Number(arrInfo[i]["bet-time"] * 1000)));
							
							_historyDesCL.txtTotal.selectable = false;
							_historyDesCL.txtP.selectable = false;
							_historyDesCL.txtT.selectable = false;
							
							_arrHistoryCL.push(_historyDesCL);
							_historyDesCL.x = (1400 - _historyDesCL.width) / 2;
							_historyDesCL.y = 370 + (i * (_historyDesCL.height + 10));
						}
					}
					
				}
			}
			
			
		}
		public function removeAllChildCL():void
		{
			_sumResultAnimal = 0;
			_sumResultCL = 0;
			var i:int;
			var j:int;
			if (_arrText && _arrText.length > 0)
			{
				for (i = 0; i < _arrText.length; i++) 
				{
					//_arrText[j].text = "";
					_arrText[i] = null;
				}
			}
			
			if (_arrText && _arrInfoTextResult.length > 0)
			{
				for (i = 0; i < _arrInfoTextResult.length; i++) 
				{
					_arrInfoTextResult[i] = null;
				}
			}
			
			if (_arrHistoryCL && _arrHistoryCL.length > 0)
			{
				for (i = 0; i < _arrHistoryCL.length; i++) 
				{
					if (_arrHistoryCL[i] != null)
					{
						removeChild(_arrHistoryCL[i]);
						_arrHistoryCL[i] = null;
					}
					
				}
			}
		}
		public function removeAllChildBetting():void
		{
			_sumResultAnimal = 0;
			_sumResultCL = 0;
			var i:int;
			var j:int;
			if (_arrText && _arrText.length > 0)
			{
				for (i = 0; i < _arrText.length; i++) 
				{
					//_arrText[j].text = "";
					_arrText[i] = null;
				}
			}
			
			if (_arrText && _arrInfoTextResult.length > 0)
			{
				for (i = 0; i < _arrInfoTextResult.length; i++) 
				{
					_arrInfoTextResult[i] = null;
				}
			}
			
			if (_arrHistoryBet && _arrHistoryBet.length > 0)
			{
				for (i = 0; i < _arrHistoryBet.length; i++) 
				{
					if (_arrHistoryBet[i] != null)
					{
						removeChild(_arrHistoryBet[i]);
						_arrHistoryBet[i] = null;
					}
					
				}
			}
			
			if (_arrCheckResult && _arrCheckResult.length > 0)
			{
				for (i = 0; i < _arrCheckResult.length; i++) 
				{
					if (_arrCheckResult[i] != null)
					{
						_arrCheckResult[i] = null;
					}
					
				}
			}
			
			if (_arrValueCheckResult && _arrValueCheckResult.length > 0)
			{
				for (i = 0; i < _arrValueCheckResult.length; i++) 
				{
					if (_arrValueCheckResult[i] != null)
					{
						_arrValueCheckResult[i] = null;
					}
					
				}
			}
		}
		
		public function removeAllEvent():void
		{
			_sumResultAnimal = 0;
			_sumResultCL = 0;
			removeAllChildCL();
			removeAllChildBetting();
			
			if (_shadow)
			{
				removeChild(_shadow);
				_shadow = null;
			}
			
			if (_btnLichSuBat)
			{
				_btnLichSuBat.removeEventListener(MouseEvent.CLICK, clickLickSuBat);
				_btnLichSuBat = null;
			}
			
			if (_btnChanLe)
			{
				_btnChanLe.removeEventListener(MouseEvent.CLICK, clickLickCL);
				_btnChanLe = null;
			}
			
			if (_btnNext)
			{
				_btnNext.removeEventListener(MouseEvent.CLICK, clickNext);
				_btnNext = null;
			}
			
			if (_btnPre)
			{
				_btnPre.removeEventListener(MouseEvent.CLICK, clickPre);
				_btnPre = null;
			}
			
			if (_btnClose)
			{
				_btnClose.removeEventListener(MouseEvent.CLICK, clickClose);
				_btnClose = null;
			}
			if (_popUpHistory)
			{
				removeChild(_popUpHistory);
				_popUpHistory = null;
			}
		}

	}

}